package com.projectName.www.service;

import com.projectName.www.po.User;
import com.projectName.www.util.String_util;
import com.projectName.www.dao.User_dao;
import com.projectName.www.util.JdbcUtils;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;


public class start extends JFrame {
    private JPanel contentPane;
    private JTextField username;
    private JTextField pasword;
    User_dao ud =new User_dao();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    start frame = new start();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public start() {
        setResizable(false);
        setTitle("登录与注册");
        setDefaultCloseOperation(1);
        setBounds(100, 100, 630, 392);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);

        JLabel lblNewLabel = new JLabel("欢迎使用外卖平台");
        lblNewLabel.setFont(new Font("宋体", Font.BOLD, 23));
        lblNewLabel.setForeground(SystemColor.textText);

        JLabel lblNewLabel_1 = new JLabel("账号：");
        lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 20));

        JLabel lblNewLabel_1_1 = new JLabel("密码：");
        lblNewLabel_1_1.setFont(new Font("宋体", Font.BOLD, 20));

        username = new JTextField();
        username.setColumns(10);

        pasword = new JTextField();
        pasword.setToolTipText("");
        pasword.setColumns(10);

        JButton btnNewButton = new JButton("登录");
        btnNewButton.setFont(new Font("宋体", Font.BOLD, 20));
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //登录
                try {
                    Login(e);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        JButton btnNewButton_1 = new JButton("注册");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //注册
                try {
                    zhuce(e);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        btnNewButton_1.setFont(new Font("宋体", Font.BOLD, 20));
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
                                .addContainerGap(167, Short.MAX_VALUE)
                                .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 277, GroupLayout.PREFERRED_SIZE)
                                .addGap(162))
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(101)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addComponent(lblNewLabel_1_1, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(pasword, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(username, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(201, Short.MAX_VALUE))
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(89)
                                .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 165, GroupLayout.PREFERRED_SIZE)
                                .addGap(86)
                                .addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 165, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(101, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(40)
                                .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(username, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                        .addComponent(lblNewLabel_1_1, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(pasword, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
                                .addGap(60)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(31, Short.MAX_VALUE))
        );
        contentPane.setLayout(gl_contentPane);
    }
//注册处理
    private void zhuce(ActionEvent e) throws Exception {
        int result=JOptionPane.showConfirmDialog(null, "是否成为商家");
        int id=1;//身份辨别

        if(result==1) {
            id=0;
        }else if(result==2)return;
        String username = this.username.getText();
        String password = this.pasword.getText();
        if (String_util.isEmpty((username))) {
            JOptionPane.showMessageDialog(null, "用户名不能为空");
            return;
        }
        if (String_util.isEmpty((password))) {
            JOptionPane.showMessageDialog(null, "密码不能为空");
            return;
        }
        User user = new User(username, password);//封装
        user.setId(id);
        try {
            Connection con = JdbcUtils.getConnection();
            int currentUser = ud.zhuce(con, user);
            if(currentUser>0&&id==1){
                JOptionPane.showMessageDialog(null,"注册成功,恭喜你成为商家");return;
            }
            else if(currentUser>0&&id==0){
                JOptionPane.showMessageDialog(null,"注册成功,恭喜你成为用户");return;
            }
            else {
                JOptionPane.showMessageDialog(null,"注册失败，该用户已存在");
            }
            return;
        } catch (Exception ex) {
            throw new RuntimeException(ex);

        }
    }

    //登录处理
    private void Login(ActionEvent e) throws Exception {
       String username= this.username.getText();
        String password=this.pasword.getText();
        if(String_util.isEmpty((username))){
            JOptionPane.showMessageDialog(null,"用户名不能为空");
            return;
        }
        if(String_util.isEmpty((password))){
            JOptionPane.showMessageDialog(null,"密码不能为空");
            return;
        }
        User user=new User(username,password);//封装
        try {
           Connection con= JdbcUtils.getConnection();
            User currentUser =ud.login(con,user);
            if(currentUser!=null&&currentUser.getId()==1){
                JOptionPane.showMessageDialog(null,"登录成功");
               new OpenBusiness(currentUser);

            } else if (currentUser!=null&&currentUser.getId()==0) {
                JOptionPane.showMessageDialog(null,"登录成功");
                new OpenBuyer(currentUser);


            } else {
                JOptionPane.showMessageDialog(null,"用户或者密码错误");
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

}
